package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HaDrugPcodelist;
import com.ruoyi.system.service.IHaDrugPcodelistService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购单自动编码Controller
 * 
 * @author 鲍小伟
 * @date 2025-11-28
 */
@RestController
@RequestMapping("/system/pcodelist")
public class HaDrugPcodelistController extends BaseController
{
    @Autowired
    private IHaDrugPcodelistService haDrugPcodelistService;

    /**
     * 查询采购单自动编码列表
     */
    @PreAuthorize("@ss.hasPermi('system:pcodelist:list')")
    @GetMapping("/list")
    public TableDataInfo list(HaDrugPcodelist haDrugPcodelist)
    {
        startPage();
        List<HaDrugPcodelist> list = haDrugPcodelistService.selectHaDrugPcodelistList(haDrugPcodelist);
        return getDataTable(list);
    }

    /**
     * 导出采购单自动编码列表
     */
    @PreAuthorize("@ss.hasPermi('system:pcodelist:export')")
    @Log(title = "采购单自动编码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HaDrugPcodelist haDrugPcodelist)
    {
        List<HaDrugPcodelist> list = haDrugPcodelistService.selectHaDrugPcodelistList(haDrugPcodelist);
        ExcelUtil<HaDrugPcodelist> util = new ExcelUtil<HaDrugPcodelist>(HaDrugPcodelist.class);
        util.exportExcel(response, list, "采购单自动编码数据");
    }

    /**
     * 获取采购单自动编码详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:pcodelist:query')")
    @GetMapping(value = "/{seqid}")
    public AjaxResult getInfo(@PathVariable("seqid") Long seqid)
    {
        return success(haDrugPcodelistService.selectHaDrugPcodelistBySeqid(seqid));
    }

    /**
     * 新增采购单自动编码
     */
    @PreAuthorize("@ss.hasPermi('system:pcodelist:add')")
    @Log(title = "采购单自动编码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HaDrugPcodelist haDrugPcodelist)
    {
        return toAjax(haDrugPcodelistService.insertHaDrugPcodelist(haDrugPcodelist));
    }

    /**
     * 修改采购单自动编码
     */
    @PreAuthorize("@ss.hasPermi('system:pcodelist:edit')")
    @Log(title = "采购单自动编码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HaDrugPcodelist haDrugPcodelist)
    {
        return toAjax(haDrugPcodelistService.updateHaDrugPcodelist(haDrugPcodelist));
    }

    /**
     * 删除采购单自动编码
     */
    @PreAuthorize("@ss.hasPermi('system:pcodelist:remove')")
    @Log(title = "采购单自动编码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{seqids}")
    public AjaxResult remove(@PathVariable Long[] seqids)
    {
        return toAjax(haDrugPcodelistService.deleteHaDrugPcodelistBySeqids(seqids));
    }

    /*
    * 先从控制器写接口
    * 获取下一个采购单号（自动生成）这里写接口的URL
    * 鲍小伟@20251128
    */
    @PreAuthorize("@ss.hasPermi('system:pcodelist:next')")
    @GetMapping("/nextNo")
    public AjaxResult nextPurchaseNo(){
        return success(haDrugPcodelistService.nextPurchaseNo());
    }

    /**
     * 获取下一个采购单号（强制最小空号）
     * 鲍小伟@20251201
     */
    @GetMapping("/nextMinNo")
    public AjaxResult nextMinPurchaseNo() {
       // return success(haDrugPcodelistService.nextMinPurchaseNo()); //这样写是返回的 {msg: 'P20251201-008', code: 200} 这样的格式，需要做一些调整 这里是返回了 string
       // 这个是返回对象的
        HaDrugPcodelist po = haDrugPcodelistService.nextMinPurchaseNoObj();
        return success(po);   // 返回整个实体

    }
    //释放装状态的接口
    @PostMapping("/sfMinNo/{seqid}")
    public  AjaxResult sfMinPurchaseNo(@PathVariable Long seqid) {
        return toAjax(haDrugPcodelistService.sfMinPurchaseNo(seqid)); //根据id来释放
    }
    //根据ID来保存的时候，保存订单号的状态@鲍小伟20251202
    @PostMapping("/useMinNo/{seqid}")
    public  AjaxResult useMinPurchaseNo(@PathVariable Long seqid) {
        return toAjax(haDrugPcodelistService.useMinPurchaseNo(seqid)); //根据id来使用
    }

}
